var sysParamGrid;
var pageObj = {type:"0100",height:[50,,45],paging:true,size:15};
var layoutContainer = create_layout(pageObj);
var dhxWins,editSysParamWin;
var oneSysParamInfo,flag=-1;

$(function(){
	sysParamGrid = layoutContainer.grid[0];
	initsysParamGrid();
	loadData();
});
//初始化服务项信息Grid
function initsysParamGrid(){
	var columnsInfo=[{header:'id',id:'id',hidden:true},
					{header:'参数ID',id:'param_code',width:100},
					{header:'参数名称',id:'param_name',width:'*'},
		         	{header:'作用业务',id:'sys_type',width:120,type:'combo'},
					{header:'作用模块',id:'biz_module',width:100,type:'combo'},
					{header:'参数类型',id:'data_type',width:80,type:'combo'},
					{header:'设定值',id:'param_value'},
					{header:'默认值',id:'default_value',width:110},
					{header:'状态',id:'status',width:80,type:'combo'},
					{header:'描述',id:'remark',width:130},
					{header:'extend_value',id:'extend_value',width:80,hidden:true},
					{header:'gmt_create',id:'gmt_create',width:80,hidden:true},
					{header:'gmt_modify',id:'gmt_modify',width:80,hidden:true},
					{header:'sort_num',id:'sort_num',width:80,hidden:true},
					{header:'operator',id:'operator',width:80,hidden:true}
			        ];
	dmjplugs.grid.createGrid({
		  sm:'ch',
		  skin:'dhx_skyblue',
		  srcc:true,
		  validate : true,
		  columns:columnsInfo
	},sysParamGrid);
	sysParamGrid.setTotal(0);
	sysParamGrid.enableEditEvents(false,false,false);
	sysParamGrid.getColumnCombo(sysParamGrid.getColIndexById('biz_module')).addOption(bizModuleComboData);
	sysParamGrid.getColumnCombo(sysParamGrid.getColIndexById('sys_type')).addOption(sysTypeComboDatas);
	sysParamGrid.getColumnCombo(sysParamGrid.getColIndexById('data_type')).addOption(dataTypeComboData);
	sysParamGrid.getColumnCombo(sysParamGrid.getColIndexById('status')).addOption(statusComboData);
	sysParamGrid.attachEvent("loadPagingData", function(first, limit){
		loadData(first, limit);
	});
	sysParamGrid.attachEvent("onRowDblClicked", function(){
		doEdit('edit');
	});
	sysParamGrid.setColumnHidden(0,true);
}

//初始化头部按钮控件
function doOnButtFormInit() {
	if (typeof (butt_Form) == 'object') {
		butt_Form.attachEvent("onButtonClick",function(name,command) {
			if (command == 'edit') //编辑
				doEdit(command);
			else if (command == 'refresh')//刷新
				doRefresh();
			else if (command == 'doUndo') //作废/恢复
				doUndo();
		});
	}
}
//加载信息数据
function loadData(first,limit){
	if (!limit) {
		sysParamGrid.setTotal(0);
		sysParamGrid.clearAll();
		first = 0;
		limit = sysParamGrid.getPageRowLimit();
	}
	var postData="pageNo="+parseInt(first/limit+1)+"&pageSize="+limit+"&pageCount="+sysParamGrid.getPageTotal();
	//查询条件
	if (typeof (search_Form) == 'object') {
		var paramName=$.trim(search_Form.getInput("paramName").value);
		if(paramName&&paramName!="") postData+="&paramName="+paramName;
		// var paramName=search_Form.getItemValue("paramName") ;
		var sysType= search_Form.getItemValue("sysType") ;
		var status=search_Form.getItemValue("status") ;
		// if(paramName&&paramName!="") postData+="&paramName="+paramName;
		if(sysType&&sysType!="") postData+="&sysType="+sysType;
		if(status&&status!="") postData+="&status="+status;
	}
	$.ajax({
        url: basePath+'param/findSysParamGrid',
        type: 'post',
        dataType: 'json',
        data:encodeURI(postData),
        success: function (data) {
			if(!data){
				$func.alert('未找到参数','red');
				return false;
			}
			if(!data.result){
				$func.alert(data.message?data.message:'未找到参数','red');
				return false;
			}
			if(data.rowCount>0) {
				sysParamGrid.parse(data.gridr, "json");
				sysParamGrid.setTotal(data.rowCount);
				sysParamGrid.uncheckAll();
			}
        },
		error:function (XMLHttpRequest, textStatus, statustText) {
			$func.alert('未找到参数','red');
			return false;
		}
    });
}

//初始化筛选查询控件
function doOnSearchFormInit(){
	if (typeof (search_Form) == "object") {
		moduleSearchInit();
		search_Form.attachEvent("onButtonClick", function (name, command) {
			if (command == 'search')
				doRefresh();
			else if (command == 'reset')
				doReset();
		});
		$('input.dhxlist_txt_textarea').keypress(function(event){
			if(event.keyCode == 13){ //绑定回车
				doRefresh();
			}
		});
		var sysType_combo=search_Form.getCombo('sysType');
		sysType_combo.addOption(sysTypeComboDatas);
		sysType_combo.setComboValue('');
		sysType_combo.readonly(true);
		var status_combo=search_Form.getCombo('status');
		status_combo.addOption(statusComboData);
		status_combo.setComboValue('');
		status_combo.readonly(true);
	}
}

function doEdit(type) {
	if (type != 'add' && type != 'edit')
		return;
	var sysParams = getCheckedRowsByGrid(sysParamGrid, sysParamGrid.getColIndexById('select'));
	if (type == 'edit') {
		if (!sysParams || sysParams.length > 1){
			$func.alert("请选择一行再编辑", "red");
			return;
		}
		oneSysParamInfo=sysParams[0];
	}

	if(type == 'edit'){
		checkParam(sysParams[0].id,sysParams[0].param_code);
	}else{
		flag=Math.floor(Math.random()*1000);
		checkParam(flag);
	}
	if (!dhxWins)dhxWins = new dhtmlXWindows();
	dhxWins.setImagePath(basePP + "js/dhtmlx/dhtmlxWindows/codebase/imgs/");
	editSysParamWin = dhxWins.createWindow("editSysParamWin", 2, 2, 350,415);
	editSysParamWin.center();
	editSysParamWin.setModal(true);
	editSysParamWin.bringToTop();
	editSysParamWin.setText("<span style=\"font-size:14px;\">" + (type == "add" ? "新增参数" : "编辑参数") + "</span>");
	editSysParamWin.denyResize();
	editSysParamWin.button("minmax1").hide();
	editSysParamWin.button("park").hide();
	editSysParamWin.attachURL(basePath + "param/openEditSysParamPage?type=" + type+"&dataType=" + oneSysParamInfo.data_type);
}

//刷新操作
function doRefresh(){
	loadData();
}

function doReset() {
	search_Form.setItemValue("paramName", '');
	search_Form.setItemValue("status",'');
	search_Form.setItemValue("sysType", '');
}

//doUndo作废恢复
function doUndo() {
	var sysParams = getCheckedRowsByGrid(sysParamGrid, sysParamGrid.getColIndexById('select'));
	if (sysParams == null || sysParams.length > 1) {
		$func.alert("请先选择一项再操作", "red");
		return;
	}
	var oper='启用',status='enabled';
	if(sysParams[0].status=='enabled'||sysParams[0].status=='publish'){
		oper='停用';
		status='disabled';
	}
	var confirmArr = "是否"+oper+"参数【"+sysParams[0].param_name+"】？ <br><font color='#ff66;'>该操作会变更参数内容！</font>";
	checkParam(sysParams[0].id,sysParams[0].functionCode);
	$func.confirm({
		content : confirmArr,
		ok : function() {
			var json ={'id':sysParams[0].id,'status':status,'paramCode':sysParams[0].param_code};
			$.ajax({
				url: basePath+'param/doUndoSysParam',
				type: 'post',
				dataType: 'json',
				contentType:"application/json",
				data:JSON.stringify(json),
				success: function (data) {
					if(!data){
						$func.alert('状态'+oper+'失败','red');
						return false;
					}
					if(!data.result){
						$func.alert(data.message?data.message:'状态'+oper+'失败','red');
						return false;
					}
					$func.alert('状态'+oper+'成功！', 'green');
					doRefresh();
				},
				error:function (XMLHttpRequest, textStatus, statustText) {
					$func.alert('状态'+oper+'失败', 'red');
				}
			});
		}
	});
}
